package com.sangfor.ssl.l3vpn.service;

import android.text.TextUtils;
import android.util.Pair;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.setting.ResourceManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/* loaded from: classes2.dex */
public class IPRcManager {
    private static final String DINGTALK_ROUTE = "1.1.1.1";
    static final int PROTO_ALL = -1;
    static final int PROTO_ICMP = 2;
    static final int PROTO_TCP = 0;
    static final int PROTO_UDP = 1;
    private static final String TAG = "IPRcManager";
    private static IPRcManager sInstance = new IPRcManager();
    private ArrayList<Map<String, Object>> mRcList;
    private ResourceManager mRcManager = ResourceManager.getInstance();

    public static IPRcManager getInstance() {
        return sInstance;
    }

    private IPRcManager() {
        this.mRcList = null;
        this.mRcList = new ArrayList<>();
    }

    public List<Pair<String, String>> getLineRoutes(List<String> list) {
        getRcRoutes();
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Pair<>("1.0.0.1", "252.255.255.255"));
        List<Pair<String, String>> removeSection = removeSection(removeSection(arrayList, "127.0.0.0", "127.255.255.255"), DINGTALK_ROUTE, DINGTALK_ROUTE);
        removeSection.add(new Pair<>(DINGTALK_ROUTE, DINGTALK_ROUTE));
        if (list != null) {
            Collections.sort(list, new Comparator<String>() { // from class: com.sangfor.ssl.l3vpn.service.IPRcManager.1
                @Override // java.util.Comparator
                public int compare(String str, String str2) {
                    if (IPRcManager.ipToLong(str) < IPRcManager.ipToLong(str2)) {
                        return -1;
                    }
                    return IPRcManager.ipToLong(str) == IPRcManager.ipToLong(str2) ? 0 : 1;
                }
            });
            removeSection = removeHosts(removeSection, list);
        }
        printRclist(removeSection);
        return removeSection;
    }

    public List<Pair<String, String>> getVpnRoutes(List<String> list) {
        List<Pair<String, String>> rcRoutes = getRcRoutes();
        if (rcRoutes == null) {
            return null;
        }
        List<Pair<String, String>> combineRclist = combineRclist(sortRclist(rcRoutes));
        combineRclist.add(new Pair<>(DINGTALK_ROUTE, DINGTALK_ROUTE));
        if (list != null) {
            Collections.sort(list, new Comparator<String>() { // from class: com.sangfor.ssl.l3vpn.service.IPRcManager.2
                @Override // java.util.Comparator
                public int compare(String str, String str2) {
                    if (IPRcManager.ipToLong(str) < IPRcManager.ipToLong(str2)) {
                        return -1;
                    }
                    return IPRcManager.ipToLong(str) == IPRcManager.ipToLong(str2) ? 0 : 1;
                }
            });
            combineRclist = removeHosts(combineRclist, list);
        }
        return removeHosts(combineRclist, list);
    }

    private List<Pair<String, String>> getRcRoutes() {
        Object obj;
        this.mRcList.clear();
        ArrayList arrayList = new ArrayList();
        ArrayList<Map<String, Object>> resouceByType = this.mRcManager.getResouceByType(2);
        if (resouceByType != null && resouceByType.size() > 0) {
            arrayList.addAll(resouceByType);
        }
        ArrayList<Map<String, Object>> resouceByType2 = this.mRcManager.getResouceByType(1);
        if (resouceByType2 != null && resouceByType2.size() > 0) {
            arrayList.addAll(resouceByType2);
        }
        ArrayList arrayList2 = new ArrayList();
        for (int i = 0; i < arrayList.size(); i++) {
            Map<String, Object> map = (Map) arrayList.get(i);
            if (map != null && (obj = map.get("ips")) != null && (obj instanceof List)) {
                List list = (List) map.get("ips");
                for (int i2 = 0; i2 < list.size(); i2++) {
                    Map map2 = (Map) list.get(i2);
                    if (map2 != null) {
                        String hostFromUrl = getHostFromUrl((String) map2.get("host_from"));
                        String hostFromUrl2 = getHostFromUrl((String) map2.get("host_to"));
                        if (!TextUtils.isEmpty(hostFromUrl) && !TextUtils.isEmpty(hostFromUrl2)) {
                            this.mRcList.add(map);
                            arrayList2.add(new Pair(hostFromUrl, hostFromUrl2));
                        }
                    }
                }
            }
        }
        return arrayList2;
    }

    public List<ResType> getL3vpnResources() {
        getRcRoutes();
        if (this.mRcList.size() == 0) {
            Log.info(TAG, "getL3vpnResources mRcList.size=0");
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < this.mRcList.size(); i++) {
            Map<String, Object> map = this.mRcList.get(i);
            List list = (List) map.get("ips");
            List list2 = (List) map.get("ports");
            if (list != null && list2 != null) {
                String str = (String) map.get("proto");
                if (!TextUtils.isEmpty(str)) {
                    int parseInt = Integer.parseInt(str);
                    String str2 = (String) map.get("svc");
                    if (str2 != null && str2.equals(ResourceManager.RC_SVR_ALLIP)) {
                        parseInt = -1;
                    }
                    if (list.size() == list2.size()) {
                        ArrayList arrayList2 = new ArrayList();
                        for (int i2 = 0; i2 < list.size(); i2++) {
                            Map map2 = (Map) list.get(i2);
                            Map map3 = (Map) list2.get(i2);
                            String hostFromUrl = getHostFromUrl((String) map2.get("host_from"));
                            String hostFromUrl2 = getHostFromUrl((String) map2.get("host_to"));
                            if (hostFromUrl != null && hostFromUrl2 != null) {
                                arrayList2.add(new ResType(hostFromUrl, hostFromUrl2, parseInt, (String) map3.get("port_from"), (String) map3.get("port_to")));
                            }
                        }
                        arrayList.addAll(arrayList2);
                    }
                }
            }
        }
        return arrayList;
    }

    public boolean isL3vpnResouces(String str, int i, int i2) {
        for (int i3 = 0; i3 < this.mRcList.size(); i3++) {
            Map<String, Object> map = this.mRcList.get(i3);
            List list = (List) map.get("ips");
            List list2 = (List) map.get("ports");
            if (list != null && list2 != null) {
                String str2 = (String) map.get("proto");
                if (TextUtils.isEmpty(str2)) {
                    continue;
                } else {
                    int parseInt = Integer.parseInt(str2);
                    String str3 = (String) map.get("svc");
                    boolean z = str3 != null && str3.equals(ResourceManager.RC_SVR_ALLIP);
                    if ((z || i < 0 || parseInt == -1 || parseInt == i2) && list.size() == list2.size()) {
                        for (int i4 = 0; i4 < list.size(); i4++) {
                            Map map2 = (Map) list.get(i4);
                            Map map3 = (Map) list2.get(i4);
                            String hostFromUrl = getHostFromUrl((String) map2.get("host_from"));
                            String hostFromUrl2 = getHostFromUrl((String) map2.get("host_to"));
                            if (hostFromUrl != null && hostFromUrl2 != null) {
                                String str4 = (String) map3.get("port_from");
                                String str5 = (String) map3.get("port_to");
                                if (ipToLong(hostFromUrl) <= ipToLong(str) && ipToLong(hostFromUrl2) >= ipToLong(str)) {
                                    if (z || i < 0 || i2 == 2) {
                                        return true;
                                    }
                                    int parseInt2 = Integer.parseInt(str4);
                                    int parseInt3 = Integer.parseInt(str5);
                                    if (parseInt2 <= i && parseInt3 >= i) {
                                        return true;
                                    }
                                }
                            }
                        }
                        continue;
                    }
                }
            }
        }
        return false;
    }

    private List<Pair<String, String>> sortRclist(List<Pair<String, String>> list) {
        Collections.sort(list, new Comparator<Pair<String, String>>() { // from class: com.sangfor.ssl.l3vpn.service.IPRcManager.3
            @Override // java.util.Comparator
            public int compare(Pair<String, String> pair, Pair<String, String> pair2) {
                String str = (String) pair.second;
                String str2 = (String) pair2.second;
                long ipToLong = IPRcManager.ipToLong((String) pair.first);
                long ipToLong2 = IPRcManager.ipToLong((String) pair2.first);
                long ipToLong3 = IPRcManager.ipToLong(str);
                long ipToLong4 = IPRcManager.ipToLong(str2);
                if (ipToLong == ipToLong2 && ipToLong3 == ipToLong4) {
                    return 0;
                }
                if (ipToLong < ipToLong2) {
                    return -1;
                }
                return (ipToLong <= ipToLong2 && IPRcManager.ipToLong(str) < IPRcManager.ipToLong(str2)) ? -1 : 1;
            }
        });
        return list;
    }

    private List<Pair<String, String>> combineRclist(List<Pair<String, String>> list) {
        int i;
        ArrayList arrayList = new ArrayList();
        for (int i2 = 0; i2 < list.size(); i2 = i + 1) {
            Pair<String, String> pair = list.get(i2);
            String str = (String) pair.first;
            String str2 = (String) pair.second;
            i = i2;
            int i3 = i2 + 1;
            String str3 = str2;
            while (true) {
                if (i3 < list.size()) {
                    Pair<String, String> pair2 = list.get(i3);
                    String str4 = (String) pair2.second;
                    if (ipToLong(str3) < ipToLong((String) pair2.first)) {
                        i = i3 - 1;
                        break;
                    }
                    if (ipToLong(str3) < ipToLong(str4)) {
                        str3 = str4;
                    }
                    i = i3;
                    i3++;
                }
            }
            arrayList.add(new Pair(str, str3));
        }
        return arrayList;
    }

    private List<Pair<String, String>> removeHosts(List<Pair<String, String>> list, List<String> list2) {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Pair<String, String> pair = list.get(i);
            String str = (String) pair.first;
            String str2 = (String) pair.second;
            for (int i2 = 0; i2 < list2.size(); i2++) {
                if (ipToLong(str) <= ipToLong(list2.get(i2)) && ipToLong(str2) >= ipToLong(list2.get(i2))) {
                    if (ipToLong(str) <= ipToLong(list2.get(i2)) - 1) {
                        arrayList.add(new Pair(str, makeIpStr(ipToLong(list2.get(i2)) - 1)));
                    }
                    str = makeIpStr(ipToLong(list2.get(i2)) + 1);
                }
            }
            if (ipToLong(str) <= ipToLong(str2)) {
                arrayList.add(new Pair(str, str2));
            }
        }
        return arrayList;
    }

    private List<Pair<String, String>> removeSection(List<Pair<String, String>> list, String str, String str2) {
        ArrayList arrayList = new ArrayList();
        boolean z = true;
        int i = 0;
        while (i < list.size() && z) {
            Pair<String, String> pair = list.get(i);
            String str3 = (String) pair.first;
            String str4 = (String) pair.second;
            if (ipToLong(str3) <= ipToLong(str) && ipToLong(str4) >= ipToLong(str)) {
                if (ipToLong(str4) <= ipToLong(str2) && ipToLong(str) - 1 >= ipToLong(str3)) {
                    arrayList.add(new Pair(str3, makeIpStr(ipToLong(str) - 1)));
                }
                if (ipToLong(str4) > ipToLong(str2)) {
                    if (ipToLong(str) - 1 >= ipToLong(str3)) {
                        arrayList.add(new Pair(str3, makeIpStr(ipToLong(str) - 1)));
                    }
                    if (ipToLong(str2) + 1 <= ipToLong(str4)) {
                        arrayList.add(new Pair(makeIpStr(ipToLong(str2) + 1), str4));
                        z = false;
                    }
                }
                i++;
            } else {
                if (ipToLong(str3) < ipToLong(str) || ipToLong(str4) > ipToLong(str2)) {
                    if (ipToLong(str4) >= ipToLong(str2) && ipToLong(str3) <= ipToLong(str2)) {
                        if (ipToLong(str2) + 1 <= ipToLong(str4)) {
                            arrayList.add(new Pair(makeIpStr(ipToLong(str2) + 1), str4));
                        }
                        z = false;
                    } else {
                        arrayList.add(new Pair(str3, str4));
                    }
                }
                i++;
            }
        }
        while (i < list.size()) {
            arrayList.add(list.get(i));
            i++;
        }
        return arrayList;
    }

    private String getHostFromUrl(String str) {
        if (str == null) {
            return null;
        }
        if (str.indexOf("://") != -1) {
            try {
                return new URI(str).getHost();
            } catch (URISyntaxException unused) {
            }
        } else if (str.indexOf(":") != -1 || str.indexOf("/") != -1) {
            return str.split("[:/]")[0];
        } else {
            if (str.indexOf("\\\\") != -1) {
                int indexOf = str.indexOf("\\\\") + 2;
                int indexOf2 = str.indexOf("\\", indexOf);
                if (indexOf2 != -1) {
                    return str.substring(indexOf, indexOf2);
                }
                return str.substring(indexOf);
            } else if (str.matches("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")) {
                return str;
            }
        }
        return null;
    }

    public static long ipToLong(String str) {
        String[] split = str.split("\\.");
        if (split.length != 4) {
            throw new IllegalArgumentException("Invalid ip str, ip = " + str);
        }
        long j = 0;
        for (String str2 : split) {
            j = (j << 8) + Integer.parseInt(str2);
        }
        return j;
    }

    public static String makeIpStr(long j) {
        return ((((((new String() + ((j >> 24) & 255)) + ".") + ((j >> 16) & 255)) + ".") + ((j >> 8) & 255)) + ".") + (j & 255);
    }

    private void printRclist(List<Pair<String, String>> list) {
        for (int i = 0; i < list.size(); i++) {
            Pair<String, String> pair = list.get(i);
            Log.info(TAG, "route: from = " + ((String) pair.first) + ", to = " + ((String) pair.second));
        }
        Log.info(TAG, ".");
    }

    private void printResTypeList(List<ResType> list) {
        for (int i = 0; i < list.size(); i++) {
            ResType resType = list.get(i);
            Log.info(TAG, "host: from = " + resType.hostFrom + ", to = " + resType.hostTo + ", port: from" + resType.portStart + ", to" + resType.portEnd + ", protocol: " + resType.protocol);
        }
    }
}
